<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>学生网课管理平台</title>
  <style>
    /* Add some styling to make the form look nice */
    body {
      font-family: sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    .login-form {
      max-width: 500px;
      margin: auto;
      padding: 20px 20px 20px 20px;
      border: 1px solid #ccc;
      border-radius: 5px;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .login-form input[type="text"],
    .login-form input[type="password"] {
      width: 95%;
      height: 36px;
      padding: 8px;
      border: 1px solid #ddd;
      border-radius: 5px;
      margin-bottom: 20px;
    }
    .login-form input[type="submit"] {
      width: 100%;
      background-color: #4CAF50;
      color: white;
      font-size: 18px;
      cursor: pointer;
      border: none;
      border-radius: 5px;
      margin-bottom: 20px;
    }
    .login-form input[type="submit"]:hover {
      background-color: #45a049;
    }
    .captcha-container {
      display: flex;
      align-items: center;
    }
    .captcha-container input[type="text"] {
      flex: 1;
      margin-right: 10px;
    }
    .captcha-container img {
      width: 100px;
      height: 36px;
      border-radius: 5px;
      cursor: pointer;
    }
    .login-form .links {
      display: flex;
      justify-content: center;
      margin-top: 20px;
    }
    .login-form .links a {
      margin: 0 10px;
    }
  </style>
</head>
<body>
  <div class="login-form">
    <form method="POST" action="/login" onsubmit="return validateForm(event)">
      <label for="username">Username:</label><br>
      <input type="text" id="username" name="username"><br>
      <label for="password">Password:</label><br>
      <input type="password" id="password" name="password"><br>
      <div class="captcha-container">
        <input type="text" id="captcha" name="captcha">
        <img src="" id="captcha-image" alt="Captcha" onclick="refreshCaptcha()">
      </div>
      <input type="submit" value="Submit">
      <div class="links">
        <a href="/register.html">Register</a>
        <a href="/forgot-password.html">Forgot Password</a>
      </div>
    </form> 
  </div>
  <script src="js/conf.js"></script>
  <script>
    function refreshCaptcha() {
      // Make a request to the /code endpoint to get a new captcha
		fetch(url+'/code',{credentials: 'include'})
		  .then(response => {
			// Check if a cookie was set in the response
			const cookie = response.headers.get('set-cookie');
			if (cookie) {
			  document.cookie=cookie;
			  // You can use the cookie here if needed
			}
			return response.text();
		  })
		  .then(text => {
			// Update the src attribute of the captcha image
			document.getElementById('captcha-image').src = 'data:image/png;base64,' + text;
		  });
    }

	function validateForm(event) {
	  // Send the form data to the /login endpoint
	  fetch(url+'/login', {
		method: 'POST',
		body: new FormData(event.target),
		credentials: 'include'
	  })
		.then(response => response.json())
		.then(data => {
		  if (data.success) {
			// The login was successful, you can redirect the user or do something else here
		  } else {
			// The login failed, show an error message
			if(data.msg=='登录成功'){location.assign('/home.html');}
			refreshCaptcha();
			alert(data.msg);
		  }
		});

	  // Prevent the form from being submitted and refreshing the page
	  event.preventDefault();
	}
  </script>
</body>
</html>
